<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "goods".
 *
 * @property int       $id
 * @property int       $status 状态
 * @property int       $type 商品类型
 * @property int       $img 图片
 * @property int       $right 权限（fk）
 * @property string    $title 商品名
 * @property string    $description 描述
 * @property string    $color 颜色
 * @property int       $price 价格
 * @property int       $created_at 创建时间
 * @property int       $updated_at 最后修改时间
 * @property int       $availability_period 有效时间
 * @property int       $availability_num 有效次数
 * @property string    $code 代号
 *
 * @property Image     $img0
 * @property GoodRight $right0
 */
class Goods extends \yii\db\ActiveRecord
{
    const TYPE_COLORS = 1;

    const STATUS_ON = 10;
    const STATUS_OFF = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'goods';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [
                [
                    'status',
                    'type',
                    'img',
                    'right',
                    'price',
                    'created_at',
                    'updated_at',
                    'availability_period',
                    'availability_num',
                ],
                'integer'
            ],
            [['title', 'description', 'color', 'code'], 'string', 'max' => 255],
            [['code'], 'unique', 'message' => "code已存在"],
            [
                ['img'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => Image::className(),
                'targetAttribute' => ['img' => 'id']
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'                  => 'ID',
            'status'              => '状态',
            'type'                => '商品类型',
            'img'                 => '图片',
            'right'               => '权限',
            'title'               => '商品名',
            'description'         => '描述',
            'color'               => '颜色',
            'price'               => '价格',
            'created_at'          => '创建时间',
            'updated_at'          => '最后修改时间',
            'availability_period' => "有效时间(月)",
            'availability_num'    => "有效次数",
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getImg0()
    {
        return $this->hasOne(Image::className(), ['id' => 'img']);
    }

    public function getRight0()
    {
        return $this->hasOne(GoodRight::className(), ['id' => 'right']);
    }

    /**
     * @param $code
     *
     * @return Goods|null
     */
    public static function findByCode($code)
    {
        return Goods::findOne(['code' => $code]);
    }

    public function beforeSave($insert)
    {
        if ($insert) {
            $this->updated_at = $this->created_at = time();
        } else {
            $this->updated_at = time();
        }

        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


}
